/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codegen.ui.nbvisual.visual;

import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;
import org.netbeans.api.visual.widget.Widget;

/**
 *
 * @author hungcuong
 */
public class VisualHelper {

    public static Widget upToFind(Widget widget, final Class aClass) {

        if (widget != null) {
            Widget parent = widget.getParentWidget();
            if (parent != null) {
                if (aClass.isInstance(parent)) {
                    return parent;
                } else {
                    return upToFind(parent, aClass);
                }

            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static Widget upToFind(Widget widget, final Class aClass, int maxInt) {
        if (widget != null) {
            Widget parent = widget.getParentWidget();
            if (parent != null || maxInt == 0) {

                if (aClass.isInstance(parent)) {
                    return parent;
                } else {
                    return upToFind(parent, aClass, maxInt - 1);
                }

            } else {
                return null;
            }

        } else {
            return null;
        }
    }

    public static Image loadImageFromResource(Class aClass, String path) {
        ClassLoader cldr = aClass.getClassLoader();
        URL imageURL = cldr.getResource(path);

        ImageIcon icon = new ImageIcon(imageURL);
        if (icon.getImage() == null) {
            System.out.println(" Null Image !");
        }
        return icon.getImage();
    }
}
